package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.mapper.MedicalDeviceMapper;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.MedicalDevice;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.MedicalDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【medical_device】的数据库操作Service实现
* @createDate 2024-12-08 17:44:53
*/
@Service
public class MedicalDeviceServiceImpl extends ServiceImpl<MedicalDeviceMapper, MedicalDevice>
    implements MedicalDeviceService{
    @Autowired
    private MedicalDeviceMapper medicalDeviceMapper;

    @Override
    public ResponseResult<Map<String, Object>> medicalDeviceFind(Map<String,Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        if (paramsMap.get("pageSize") != null || paramsMap.get("currentPage") != null){
            Integer pageSize = (Integer) paramsMap.get("pageSize");
            paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
            responseResult.setPageSize(pageSize);
        }
        List<Map<String, Object>> healthInstances = medicalDeviceMapper.findMedicalDevices(paramsMap);
        Integer total = medicalDeviceMapper.findMedicalDevicesNum(paramsMap);

        if (healthInstances != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗器械信息成功");
            responseResult.setDatas(healthInstances);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗器械信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> medicalDeviceUpdate(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        Integer rows = medicalDeviceMapper.updateMedicalDevice(paramsMap);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗器械信息更新成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗器械信息更新失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> medicalDeviceAdd(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        MedicalDevice medicalDevice = new MedicalDevice();

        medicalDevice.setName((String) paramsMap.get("name"));
        medicalDevice.setDescription((String) paramsMap.get("description"));
        medicalDevice.setDeviceImg((String) paramsMap.get("device_img"));
        boolean save = this.save(medicalDevice);
        if (save) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗器械添加成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗器械添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> medicalDeviceDelete(Integer institutionId) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<MedicalDevice> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MedicalDevice::getId,institutionId);
        wrapper.set(MedicalDevice::getIsDel,1);
        boolean update = this.update(wrapper);
        if (update) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗器械删除成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗器械删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> deviceDropDownFind(Integer institutionId) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> devices = medicalDeviceMapper.findDevicesDropDown(institutionId);
        if (devices != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗器械下拉框成功");
            responseResult.setDatas(devices);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗器械下拉框失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> deviceDropDownWithoutFind(Integer institutionId) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> devices = medicalDeviceMapper.findDevicesDropDownWithout(institutionId);
        if (devices != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗器械下拉框成功");
            responseResult.setDatas(devices);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗器械下拉框失败");
        }
        return responseResult;
    }
}




